import 'dart:async';
import '../function.dart';

bool loaded = false;
int decimal = 2;

class PriceModel {
  String name;
  String type;
  String invite;
  String current = "";
  String max = "";
  String min = "";
  String change = "";
  String amount = "";
  bool upDown = true;
  PriceModel(this.name, this.type, this.invite, {this.current, this.max, this.min, this.change});
  Future<Null> getPrice() async {
    Map data;
    switch (this.type) {
      case "wkj":
        data = await getWkjPrice();
        break;
      case "uyulin":
        data = await getUyulinPrice();
        break;
      case "fatbtc":
        data = await getFatbtcPrice();
        break;
      case "zxx":
        data = await getZxxPrice();
        break;
      case "playwkc":
        data = await getPlayWkcPrice();
        break;
      case "6lk":
        data = await get6lkPrice();
        break;
      case "66otc":
        data = await get66otcPrice();
        break;
      case "firefox":
        data = await getFirefoxPrice();
        break;
      case "51jd":
        data = await get51jdPrice();
        break;
    }
    if (data != null) {
      this.current = data['current'] ?? "";
      this.max = data['max'] ?? "";
      this.min = data['min'] ?? "";
      this.upDown = data['upDown'] ?? false;
      this.amount = data['amount'] ?? "";
      this.change = data['change'] ?? "";
    }
  }
}

List<PriceModel> platforms = [
  new PriceModel("玩客家", "wkj", "http://www.wkj.link/?invit=XQRBZN"),
  //new PriceModel("悠雨林","uyulin","http://beenl.pro"),
  new PriceModel("胖比特", "fatbtc", "https://www.fatbtc.us/register?uid=u101117855"),
  new PriceModel("Z网", "zxx", "http://zxx.cm/login/register/invit/QEWGVN.html"),
  new PriceModel("PlayWKC", "playwkc", "https://www.playwkc.com/register?ref=DoznqZ0xyb"),
  new PriceModel("6lk.com", "6lk", "https://www.6lk.com/register?ref=5dK0nWMVjr"),
  new PriceModel("66OTC", "66otc", "https://www.66otc.com/register?ref=VpakGXVVaP"),
  new PriceModel("火狐狸", "firefox", "https://firefoxotc.com/trade?referral_code=PmcPUQxXCFUK"),
  //new PriceModel("蓝枫林", "51jd", "http://www.51jd.ink/?invit=RJ924677"),
];
Future<Map> getWkjPrice() async {
  var url = "http://www.wkj.link/Ajax/getJsonTop?market=wkb_cny";
  var header = {
    "Upgrade-Insecure-Requests": "1",
    "Cookie": "UM_distinctid=16517207cb99f5-074215f28b16d9-2711639-384000-16517207cba4a0; CNZZDATA1271250422=1490304391-1533687532-%7C1533687532; aliyungf_tc=AQAAALK7wRPbnwUAc7imd8TtYBTBmZAZ; acw_tc=AQAAANSq+BFPtgQAc7imd4+ycB9d7yge; think_language=zh-CN; PHPSESSID=nmprk7ll9c31nf9lsgeaetlos3",
  };
  var res = await get(url, null, null);
  if (res == null) {
    return null;
  }
  var data = res['info'];
  Map price = {
    "current": data["new_price"] != null ? data["new_price"].toStringAsFixed(decimal) : "-",
    "upDown": data["change"] != null ? data["change"] < 0 ? false : true : false,
    "max": data['max_price'] != null ? data['max_price'].toStringAsFixed(decimal) : "-",
    "min": data['min_price'] != null ? data['min_price'].toStringAsFixed(decimal) : "-",
    "amount": data['volume'] != null ? (data['volume'] / 10000).toStringAsFixed(decimal) + "万" : "-",
    "change": data['change'] != null ? data['change'].toStringAsFixed(decimal) : "-",
  };
  return price;
}

Future<Map> getUyulinPrice() async {
  var url = "http://beenlapi.moduointerface.net/public/allMarketTradeData";
  var header = {"X-Custom-UserAgent": "vbourse/v1.1.0", "Cache-Control": "no-cache", "Origin": "http://beenl.pro", "Referer": "http://beenl.pro/"};
  var res = await post(url: url, data: {"sortFlag": "false"}, header: header, contentType: "application/x-www-form-urlencoded");
  if (res == null) {
    return null;
  }
  var data = res['body'][1];
  Map price = {
    "current": data["closePrice"].toStringAsFixed(decimal),
    "upDown": data["mchange"] < 0 ? false : true,
    "max": data['maxPrice'].toStringAsFixed(decimal),
    "min": data['minPrice'].toStringAsFixed(decimal),
    "amount": (data['volume'] / 10000).toStringAsFixed(decimal) + "万",
    "change": data['mchange'].toStringAsFixed(decimal),
  };
  return price;
}

Future<Map> getFatbtcPrice() async {
  var time = new DateTime.now().millisecondsSinceEpoch;
  var url = "https://www.fatbtc.us/m/allticker/1/$time";
  var res = await get(url, null, null);
  if (res == null) {
    return null;
  }
  var data = res["data"]["wkcfcny_ticker"];
  Map price = {
    "current": data["close"].toStringAsFixed(decimal),
    "upDown": (data['close'] - data['open']) < 0 ? false : true,
    "max": data['high'].toStringAsFixed(decimal),
    "min": data['low'].toStringAsFixed(decimal),
    "amount": (data['amount'] / 10000).toStringAsFixed(decimal) + "万",
    "change": ((data['close'] - data['open']) / data['open'] * 100).toStringAsFixed(decimal),
  };
  return price;
}

Future<Map> getZxxPrice() async {
  var url = "https://www.fmtol.com/Home/Index/getZxxPrice";
  var res = await get(url, null, null);
  if (res == null) {
    return null;
  }
  String current = res['current'];
  if (current == null) {
    return null;
  }
  Map price = {
    "current": double.parse(current.substring(1)).toStringAsFixed(decimal),
    "upDown": res["daily"] < 0 ? false : true,
    "max": (res['high'] * 6.7).toStringAsFixed(decimal),
    "min": (res['low'] * 6.7).toStringAsFixed(decimal),
    "amount": (res['amount'] / 10000).toStringAsFixed(decimal) + "万",
    "change": res['daily'].toStringAsFixed(decimal),
  };
  return price;
}

Future<Map> getPlayWkcPrice() async {
  var url = "https://www.playwkc.com/api/wkc.php";
  var res = await get(url, null, null);
  if (res == null) {
    return null;
  }
  Map price = {
    "current": double.parse(res['new_price']).toStringAsFixed(decimal),
    "upDown": true,
    "max": double.parse(res['max_price']).toStringAsFixed(decimal),
    "min": double.parse(res['min_price']).toStringAsFixed(decimal),
    "amount": (double.parse(res['amount']) / 10000).toStringAsFixed(decimal) + "万",
    "change": "-"
  };
  return price;
}

Future<Map> get6lkPrice() async {
  var url = "https://www.6lk.com/api/ticker";
  var res = await get(url, null, null);
  if (res == null) {
    return null;
  }
  var data = res["ticker"];
  Map price = {
    "current": double.parse(data['last']).toStringAsFixed(decimal),
    "upDown": double.parse(data["change"]) < 0 ? false : true,
    "max": double.parse(data['high']).toStringAsFixed(decimal),
    "min": double.parse(data['low']).toStringAsFixed(decimal),
    "amount": (double.parse(data['vol']) / 10000).toStringAsFixed(decimal) + "万",
    "change": data['change']
  };
  return price;
}

Future<Map> get66otcPrice() async {
  var url = "https://api.66otc.com/api/ticker?symbol=WKC";
  var res = await get(url, null, null);
  if (res == null) {
    return null;
  }
  var data = res["ticker"];
  Map price = {
    "current": double.parse(data['last']).toStringAsFixed(decimal),
    "upDown": double.parse(data["change"]) < 0 ? false : true,
    "max": double.parse(data['high']).toStringAsFixed(decimal),
    "min": double.parse(data['low']).toStringAsFixed(decimal),
    "amount": (double.parse(data['vol']) / 10000).toStringAsFixed(decimal) + "万",
    "change": data['change']
  };
  return price;
}

Future<Map> getFirefoxPrice() async {
  var header = {
    "Cache-Control": "max-age=0",
  };
  var url = "https://www.firefoxotc.com/api/exchange/ticker_list";
  var res = await post(url: url, data: {"currency": "FFC"}, header: header, contentType: "application/x-www-form-urlencoded");
  if (res == null) {
    return null;
  }
  var data = res['data']['tickers']['OSC_FFC'];
  Map price = {"current": data['last'].toStringAsFixed(decimal), "upDown": true, "max": data['high'].toStringAsFixed(decimal), "min": data['low'].toStringAsFixed(decimal), "amount": (data['volume'] / 10000).toStringAsFixed(decimal) + "万", "change": "-"};
  return price;
}

Future<Map> get51jdPrice() async {
  var url = "https://www.51jd.ink/trade/index_json/market/lk_doge";
  var res = await get(url, null, null);
  if (res == null) {
    return null;
  }
  var data = res['top'];
  Map price = {
    "current": double.parse(data[0]).toStringAsFixed(decimal),
    "upDown": double.parse(data[6]) < 0 ? false : true,
    "max": double.parse(data[4]).toStringAsFixed(decimal),
    "min": double.parse(data[3]).toStringAsFixed(decimal),
    "amount": (double.parse(data[5]) / 10000).toStringAsFixed(decimal) + "万",
    "change": double.parse(data[6]).toStringAsFixed(decimal),
  };
  return price;
}
